package test.data;

import java.sql.Time;
import java.util.Date;
import java.util.Vector;

import com.roadmap.common.BaseConst;
import com.roadmap.common.BaseEntity;
import com.roadmap.common.BaseUtility;
import com.roadmap.entities.*;
import com.roadmap.repo.db.DatabaseUtility;
import com.roadmap.repo.db.IDatabaseUtility;

public class DataBuilder {
	private final IDatabaseUtility dbutil = new DatabaseUtility();
	private final String[] mails = { "chen@163.com", "hao231@he.com",
			"12891@163.com" };
	private final String password = "roadmap";
	private final String[] IPs = { "9.123.122.34", "231.109.108.4",
			"168.111.90.214" };

	private final int reg_count = 3;
	private final int asp_count = 2;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			DataBuilder.getInstance().loadAllData();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public static DataBuilder getInstance() {
		return new DataBuilder();
	}

	public void loadAllData() throws Exception {
		try {
			Object[] regs = loadRegs();
			Object[] asps = loadAspirants(regs);
			loadMap(asps);
			Object[] cates = loadCategory((Aspirant)asps[0]);
			LifeGoal goal = loadGoal(((Aspirant)asps[0]).getId(), ((Category)cates[0]).getName());
			loadRisk(goal.getId());
			loadResource(goal.getId());
			LifePlan plan = loadPlan(goal);
			loadMilestones(plan.getId());
			Object[] tasks = loadTask(((Aspirant)asps[0]).getId(), plan.getId());
			loadExec(((LifeTask)tasks[0]).getId());
			loadAchi(((Aspirant)asps[0]).getId());
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private Object[] loadRegs() throws Exception {
		Vector<BaseEntity> regs = new Vector<BaseEntity>();
		for (int i = 0; i < reg_count; i++) {
			Registration reg = new Registration();
			reg.setMail(mails[i]);
			reg.setIpAddress(IPs[i]);
			reg.setRandomCode(BaseUtility.getRandomString());
			regs.add(reg);
		}
		dbutil.saveOrUpdateEntities(regs);
		return regs.toArray();

	}

	private Object[] loadAspirants(Object[] regs) throws Exception {
		Date[] dates = new Date[asp_count];
		dates[0] = BaseUtility.parseStringToDate("1982-3-5");
		dates[1] = BaseUtility.parseStringToDate("1989-12-5");
		// dates[2] = BaseUtility.parseStringToDate("1990-10-25");
		Aspirant[] asps = new Aspirant[asp_count];
		for (int i = 0; i < asp_count; i++) {
			Registration reg = (Registration) regs[i];
			asps[i] = new Aspirant();
			asps[i].setMail(reg.getMail());
			asps[i].setBirthday(dates[i]);
			asps[i].setRealName("Eric " + i);
			char sex = BaseConst.ASPIRANT_SEX_MALE;
			if (i % 2 == 1)
				sex = BaseConst.ASPIRANT_SEX_FEMALE;
			asps[i].setSex(sex);
			asps[i].setLoginPassword(password);
			asps[i].setLoginIP(IPs[i]);
			reg.setStatus(BaseConst.REGISTRATION_STATUS_Passed);
		}

		Vector<BaseEntity> vec = new Vector<BaseEntity>();
		Vector<BaseEntity> vecAsp = new Vector<BaseEntity>();
		vecAsp.add(asps[0]);
		vecAsp.add(asps[1]);
		vec.addAll(vecAsp);
		Registration reg = (Registration) regs[0];
		vec.add(reg);
		reg = (Registration) regs[1];
		vec.add(reg);

		dbutil.saveOrUpdateEntities(vec);

		return vecAsp.toArray();
	}

	private void loadMap(Object[] asps) throws Exception {
		Vector<BaseEntity> vec = new Vector<BaseEntity>();
		for (int i = 0; i < asp_count; i++) {
			
			Aspirant asp = (Aspirant) asps[i];
			LifeMap map = new LifeMap();
			map.setId(asp.getId());
			map.setLifeDream("成为世界第" + i + 1);
			map.setPersonalFaith("我相信努力一定会成功！");
			map.setDreamContent("将来某天我在做喜欢的事。");
			vec.add(map);
		}
		dbutil.saveOrUpdateEntities(vec);
	}

	private Object[] loadCategory(Aspirant asp) throws Exception {
		Vector<BaseEntity> vec = new Vector<BaseEntity>();
		Category cate1 = new Category();
		cate1.setName("体育");
		cate1.setOwnerId(asp.getId());
		vec.add(cate1);
		Category cate2 = new Category();
		cate2.setOwnerId(asp.getId());
		cate2.setName("Animal");
		vec.add(cate2);
		dbutil.saveOrUpdateEntities(vec);
		return vec.toArray();
	}

	private LifeGoal loadGoal(String aspId, String catName) throws Exception {
		LifeGoal goal = new LifeGoal();
		goal.setAspirantId(aspId);
		// goal.setCategoryId(catName);
		goal.setPriority(BaseConst.ENTITY_PRIORITY_MEDIUM);
		goal.setName("为了梦想");

		Date dt = BaseUtility.parseStringToDate("2009-9-1");
		goal.setScheduledEndDate(dt);
		goal.setContent("做一個有用的人；做十件有價值的事；become a great man");
		goal.setStatus(BaseConst.LIFEGOAL_STATUS_UNDERGOING);

		dbutil.save(goal);

		return goal;
	}

	private void loadRisk(String goalId) throws Exception {
		Risk rk = new Risk();
		rk.setGoalId(goalId);
		rk.setContent("金融危机");
		dbutil.save(rk);
	}

	private void loadResource(String goalId) throws Exception {
		Resource res = new Resource();
		res.setContent("我有一本Java实战书");
		res.setGoalId(goalId);
		dbutil.save(res);
	}

	private LifePlan loadPlan(LifeGoal goal) throws Exception {
		LifePlan plan = new LifePlan();
		plan.setAspirantId(goal.getAspirantId());
		plan.setContent("1.看完一本书；2.多做习题；3.通过考试");
		plan.setName("诺曼底登陆计划");
		plan.setScheduledEndDate(goal.getScheduledEndDate());
		plan.setScheduledStartDate(new Date());
		plan.setGoalId(goal.getId());
		dbutil.save(plan);

		return plan;
	}

	private void loadMilestones(String planId) throws Exception {
		Vector<BaseEntity> vec = new Vector<BaseEntity>();
		Date[] dates = new Date[3];
		dates[0] = BaseUtility.parseStringToDate("2009-10-5");
		dates[1] = BaseUtility.parseStringToDate("2009-12-19");
		dates[2] = BaseUtility.parseStringToDate("2010-2-20");

		for (int i = 0; i < 3; i++) {
			Milestone stone = new Milestone();
			stone.setContent("里程碑之" + i + 1);
			stone.setPlanId(planId);
			stone.setProportionOfPlan(10 + i * 30);
			int st = (i == 1) ? st = BaseConst.MILESTONE_STATUS_SATISFIED
					: BaseConst.MILESTONE_STATUS_UNSATISFIED;
			stone.setStatus(st);
			stone.setStoneDate(dates[i]);
			vec.add(stone);
		}

		dbutil.saveOrUpdateEntities(vec);
	}

	private Object[] loadTask(String aspId, String planId) throws Exception {
		Date[] dates = new Date[3];
		dates[0] = BaseUtility.parseStringToDateTime("2009-09-10 09:30");
		dates[1] = BaseUtility.parseStringToDateTime("2009-12-15 09:30");
		dates[2] = BaseUtility.parseStringToDateTime("2010-02-20 09:30");

		LifeTask[] tasks = new LifeTask[2];
		tasks[0] = new LifeTask();
		tasks[0].setAspirantId(aspId);
		tasks[0].setContent("读完两本书");
		tasks[0].setExecuteCycle(3);

		tasks[0].setScheduledStartDate(dates[0]);
		tasks[0].setScheduledEndDate(dates[1]);
		tasks[0].setProportionOfGoal(40);
		tasks[0].setStatus(BaseConst.LIFETASK_STATUS_UNDERGOING);

		tasks[1] = new LifeTask();
		tasks[1].setAspirantId(aspId);
		tasks[1].setContent("完成两道超级难的题");
		tasks[1].setExecuteCycle(4);
		tasks[1].setScheduledStartDate(dates[2]);
		tasks[1].setScheduledEndDate(dates[2]);
		tasks[1].setProportionOfGoal(60);

		tasks[0].setGoalId(planId);
		tasks[1].setGoalId(planId);
		
		Vector<BaseEntity> vec = new Vector<BaseEntity>();
		vec.add(tasks[0]);
		vec.add(tasks[1]);
		dbutil.saveOrUpdateEntities(vec);

		return vec.toArray();
	}

	private void loadExec(String taskId) throws Exception {
		Execution exec = new Execution();
		exec.setContent("今天看了100页");
		exec.setTaskId(taskId);
		exec.setProportionOfTask(28);
		Date dt = BaseUtility.parseStringToDate("2009-09-10");
		exec.setExecuteDate(dt);
		dbutil.save(exec);
	}
	
	private void loadAchi(String aspirantId) throws Exception{
		Achievement achi = new Achievement();
		achi.setAspirantId(aspirantId);
		achi.setContent("当选为最佳学生干部");
		Date dt = BaseUtility.parseStringToDate("2001-10-01");
		achi.setHappenDate(dt);
		dbutil.save(achi);
	}
}
